package canadaposttest;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Properties;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.ws.security.WSConstants;
import org.apache.ws.security.WSPasswordCallback;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.security.wss4j.WSS4JOutHandler;
import org.codehaus.xfire.util.dom.DOMOutHandler;

import ca.canadapost.ws.soap.ship.rate.v2.GetRatesRequest;
import ca.canadapost.ws.soap.ship.rate.v2.GetRatesResponse;
import ca.canadapost.ws.soap.ship.rate.v2.Messages;
import ca.canadapost.ws.soap.ship.rate.v2.RatingPortType;
import ca.canadapost.ws.soap.ship.rate.v2.RatingServiceClient;
import ca.canadapost.ws.soap.ship.rate.v2.GetRatesRequest.MailingScenario;
import ca.canadapost.ws.soap.ship.rate.v2.GetRatesRequest.MailingScenario.Destination;
import ca.canadapost.ws.soap.ship.rate.v2.GetRatesRequest.MailingScenario.ParcelCharacteristics;
import ca.canadapost.ws.soap.ship.rate.v2.GetRatesRequest.MailingScenario.Destination.Domestic;
import ca.canadapost.ws.soap.ship.rate.v2.GetRatesResponse.PriceQuotes;
import ca.canadapost.ws.soap.ship.rate.v2.GetRatesResponse.PriceQuotes.PriceQuote;
import ca.canadapost.ws.soap.ship.rate.v2.Messages.Message;

/**
 * XFire client
 * @author wei
 *
 */
public class CPCTest implements CallbackHandler {
    
    private static final String Sandbox_URL="https://ct.soa-gw.canadapost.ca/rs/soap/rating/v2";
    private static final String Live_URL="https://soa-gw.canadapost.ca/rs/soap/rating/v2";
    
    public void go(){
        RatingServiceClient rsClient =new RatingServiceClient();
        RatingPortType ratingService=rsClient.getRatingPort(Sandbox_URL);
        
        Client xfclient = Client.getInstance(ratingService);
        xfclient.addOutHandler(new DOMOutHandler());
        
        Properties config = new Properties();
        // Action to perform : user token
        config.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
        // Password type : plain text
        config.setProperty(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
        
        config.setProperty(WSHandlerConstants.USER, "xx");
        config.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, this.getClass().getName());
        
        
        
        xfclient.addOutHandler(new WSS4JOutHandler(config));
        
        GetRatesRequest request=new GetRatesRequest();
        
        MailingScenario ms = new MailingScenario();
        request.setMailingScenario(ms);
        ms.setCustomerNumber("xx");
        ms.setContractId("0");
        
        ParcelCharacteristics pc =new ParcelCharacteristics();
        ms.setParcelCharacteristics(pc);
        pc.setWeight(new BigDecimal("1"));
        
        ms.setOriginPostalCode("V6B4A2");
        
        Destination des = new Destination();
        Domestic dom =new Domestic();
        des.setDomestic(dom);
        dom.setPostalCode("J0E1X0");
        
        ms.setDestination(des);
        
        
        GetRatesResponse response = ratingService.getRates(request);
        Messages msgs = response.getMessages();
        if(msgs!=null){
            for(Message msg : msgs.getMessage()){
                out(msg.getCode()+":"+msg.getDescription());
            }
        }
        
        PriceQuotes priceQuotes=response.getPriceQuotes();
        for(PriceQuote pq : priceQuotes.getPriceQuote()){
            out(pq.getServiceCode()+":"+pq.getServiceName()+":"+pq.getPriceDetails().getBase());
        }
        
        
    }
    
    private void out(Object obj){
        System.out.println("!!!!!! "+obj);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        CPCTest test=new CPCTest();
        test.go();
        
    }
    
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            out("callback "+i+" "+callbacks[i].getClass().getName());
            if (callbacks[i] instanceof WSPasswordCallback) {
                WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
                out("###### Before");
                out("getCustomToken "+pc.getCustomToken());
                out("getIdentifer "+pc.getIdentifer());
                out("getKey "+pc.getKey());
                out("getPassword "+pc.getPassword());
                out("getPasswordType "+pc.getPasswordType());
                out("getUsage "+pc.getUsage());
                
                if (pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN) {
                    pc.setPassword("xx");
                }
            } else {
                throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
            }
        }
    }

}
